﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace RISMOTAXDP.EmailService.MailProcess
{
    /// <summary>
    /// 账号密码生成邮件通知 （Generate Password Email Notification）
    /// </summary>
    public class MailAccountPasswordNotifyProcess : MailProcessBase
    {

        /// <summary>
        /// 业务类型代码
        /// </summary>
        public const string MailOPCode = "A2";

        /// <summary>
        /// 获得邮件业务类型代码
        /// </summary>
        /// <returns></returns>
        public override string GetMailOPCode()
        {
            //throw new NotImplementedException();
            return MailOPCode;
        }

        /// <summary>
        /// 执行指定邮件的生成处理
        /// </summary>
        /// <param name="index">索引号(0计数)</param>
        /// <returns></returns>
        public override bool Process(int index)
        {
            //throw new NotImplementedException();
            //从TBL_MailForward表中获得邮件
            DataTable MailForwardData = m_DataSet.Tables[0];

            //Mail_ID,OperationCode,MailSubject,MailRcpt,MailContent,MailCreateDate,MailFrom 

            Guid Mail_ID = new Guid(MailForwardData.Rows[index]["Mail_ID"].ToString());

            string OperationCode = MailForwardData.Rows[index]["OperationCode"].ToString();

            string MailSubject = MailForwardData.Rows[index]["MailSubject"].ToString();

            string MailRcpt = MailForwardData.Rows[index]["MailRcpt"].ToString();

            string MailContent = MailContentProcess(MailForwardData.Rows[index]["MailContent"].ToString());

            DateTime MailCreateDate = (DateTime)MailForwardData.Rows[index]["MailCreateDate"];

            //保存为待发送邮件
            base.SaveMail(Mail_ID, OperationCode, MailSubject, MailRcpt, MailContent, MailCreateDate);
            //移除前置邮件
            base.RemoveForwardMail(Mail_ID);

            return true;
        }

        /// <summary>
        /// 处理邮件内容
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private string MailContentProcess(string code)
        {

            //获得值并分割参数值
            //参数格式:   
            //账号|密码|到期日(yyyy-mm-dd)

            string[] parameterCode = UtilHelper.SplitString(code.Trim(), "|", 3);

            string _memberID = parameterCode[0].Trim();
            string _memberPassword = parameterCode[1].Trim();
            string _memberExpireDay = parameterCode[2].Trim();

            //邮件模板内容
            string _mailContent = m_MailTemplateContent;

            //处理$MemberID$
            _mailContent = _mailContent.Replace(MailTemplateObject.MemberID, _memberID);
            //处理$MemberPassword$
            _mailContent = _mailContent.Replace(MailTemplateObject.MemberPassword, _memberPassword);
            //处理$MemberExpireDay$
            _mailContent = _mailContent.Replace(MailTemplateObject.MemberExpireDay, _memberExpireDay);

            return _mailContent;

        }

    }


}
